package acm.蓝桥杯15;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.StringTokenizer;

public class 心跳文学部 {

    //    static Comparator<Node> comparator = (a, b) -> {
//        if (a.score == b.score) {
//            return a.name.compareTo(b.name);
//        }
//        return b.score - a.score;
//    };
    static Comparator<Node> comparator = (a, b) -> {
        if (a.score == b.score) {
            return b.name.compareTo(a.name);
        }
        return (int) (a.score - b.score);
    };
    static PriorityQueue<Node> queue = new PriorityQueue<>(comparator);

    public static void main(String[] args) {
        Reader sc = new Reader();
        int n = sc.nextInt();
        while (n-- > 0) {
            int count = sc.nextInt();
            int m = sc.nextInt();
            while (count-- > 0) {
                Node node = new Node();
                node.name = sc.next();
                node.score = sc.nextInt();
                if (queue.size() < m) {
                    queue.add(node);
                } else {
//                    if (queue.peek()<node.score) {
//                        queue.poll();
//                        queue.add(node);
//                    }
                    if (comparator.compare(node, queue.peek()) > 0) {
                        queue.poll();
                        queue.add(node);
                    }
                }
            }
            int i = 1;
            StringBuilder builder = new StringBuilder();
            Stack<String> stack = new Stack<>();
            long res = 0;
            while (!queue.isEmpty()) {
                Node poll = queue.poll();
                stack.push(poll.name);
                res += poll.score * i++;
            }
            while (!stack.isEmpty()) {
                builder.append(" ").append(stack.pop());
            }
            System.out.println(res + builder.toString());
        }
    }

    static class Node {
        String name;
        long score;

    }

    static
    class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }

        public void clear() {
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}
